
import java.rmi.*;
import java.rmi.registry.*;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Mario
 */
public class rmiPeticionServidor {

     private static interfazComunicacionServidor rmiComunicacionServidor;
     private static Registry registro;
     private static String ipServidor;
     private static int puertoServidor;

     public rmiPeticionServidor(String ipServidor){
         this.ipServidor = ipServidor;
         puertoServidor = 3231;
     }

     public static boolean conexionServidor() {
        try {
            registro = LocateRegistry.getRegistry(ipServidor, (new Integer(puertoServidor)).intValue());
            rmiComunicacionServidor = (interfazComunicacionServidor) (registro.lookup(ipServidor));
            return true;
        } catch (NotBoundException ex) {
            System.out.println(ex);
            return false;
        } catch (RemoteException ex) {
            System.out.println(ex);
            return false;
        }
    }

     public List<Servicio> obtenerListaServicios(){
        try {
            List<Servicio> listaServicio = rmiComunicacionServidor.obtenerListaServicio();
            return listaServicio;
        } catch (RemoteException ex) {
            Logger.getLogger(rmiPeticionServidor.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(rmiPeticionServidor.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
     }
     
     public void print(List<Servicio> list){
         Iterator ite = list.iterator();
         while(ite.hasNext()){
             Servicio str = (Servicio)ite.next();
             //Servicio ser = ite.getClass();
             System.out.println(str.getName_servicio());
         }             
     }

     public boolean ejecutarServicio(int num){
        try {
            rmiComunicacionServidor.ejecutarServicio(num);
            return true;
        } catch (RemoteException ex) {
            Logger.getLogger(rmiPeticionServidor.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(rmiPeticionServidor.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
     }

     public static void main(String[] args) {
        rmiPeticionServidor rmi = new rmiPeticionServidor("192.168.1.33");
        rmi.conexionServidor();
        List<Servicio> list = rmi.obtenerListaServicios();
        rmi.print(list);
        boolean bool = rmi.ejecutarServicio(8);
        rmi.ejecutarServicio(7);
        rmi.ejecutarServicio(6);
        System.out.println(bool);
    }
}
